

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>packnet_sfm.losses.multiview_photometric_loss &mdash; PackNet-SfM 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../configs/configs.html">Configs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../scripts/scripts.html">Scripts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trainers/trainers.html">Trainers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../datasets/datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../networks/networks.html">Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../losses/losses.html">Losses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../loggers/loggers.html">Loggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../geometry/geometry.html">Geometry</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils/utils.html">Utils</a></li>
</ul>
<p class="caption"><span class="caption-text">Contact</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://tri.global">Toyota Research Institute</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/packnet-sfm">PackNet-SfM GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/DDAD">DDAD GitHub</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">PackNet-SfM</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>packnet_sfm.losses.multiview_photometric_loss</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for packnet_sfm.losses.multiview_photometric_loss</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Toyota Research Institute.  All rights reserved.</span>

<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="kn">from</span> <span class="nn">packnet_sfm.utils.image</span> <span class="kn">import</span> <span class="n">match_scales</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.geometry.camera</span> <span class="kn">import</span> <span class="n">Camera</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.geometry.camera_utils</span> <span class="kn">import</span> <span class="n">view_synthesis</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.depth</span> <span class="kn">import</span> <span class="n">calc_smoothness</span><span class="p">,</span> <span class="n">inv2depth</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.losses.loss_base</span> <span class="kn">import</span> <span class="n">LossBase</span><span class="p">,</span> <span class="n">ProgressiveScaling</span>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="SSIM"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.SSIM">[docs]</a><span class="k">def</span> <span class="nf">SSIM</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">C1</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span> <span class="n">C2</span><span class="o">=</span><span class="mf">9e-4</span><span class="p">,</span> <span class="n">kernel_size</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">stride</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Structural SIMilarity (SSIM) distance between two images.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    x,y : torch.Tensor [B,3,H,W]</span>
<span class="sd">        Input images</span>
<span class="sd">    C1,C2 : float</span>
<span class="sd">        SSIM parameters</span>
<span class="sd">    kernel_size,stride : int</span>
<span class="sd">        Convolutional parameters</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    ssim : torch.Tensor [1]</span>
<span class="sd">        SSIM distance</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">pool2d</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">AvgPool2d</span><span class="p">(</span><span class="n">kernel_size</span><span class="p">,</span> <span class="n">stride</span><span class="o">=</span><span class="n">stride</span><span class="p">)</span>
    <span class="n">refl</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">ReflectionPad2d</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">refl</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">refl</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
    <span class="n">mu_x</span> <span class="o">=</span> <span class="n">pool2d</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="n">mu_y</span> <span class="o">=</span> <span class="n">pool2d</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

    <span class="n">mu_x_mu_y</span> <span class="o">=</span> <span class="n">mu_x</span> <span class="o">*</span> <span class="n">mu_y</span>
    <span class="n">mu_x_sq</span> <span class="o">=</span> <span class="n">mu_x</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
    <span class="n">mu_y_sq</span> <span class="o">=</span> <span class="n">mu_y</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>

    <span class="n">sigma_x</span> <span class="o">=</span> <span class="n">pool2d</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> <span class="o">-</span> <span class="n">mu_x_sq</span>
    <span class="n">sigma_y</span> <span class="o">=</span> <span class="n">pool2d</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> <span class="o">-</span> <span class="n">mu_y_sq</span>
    <span class="n">sigma_xy</span> <span class="o">=</span> <span class="n">pool2d</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="n">mu_x_mu_y</span>
    <span class="n">v1</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">sigma_xy</span> <span class="o">+</span> <span class="n">C2</span>
    <span class="n">v2</span> <span class="o">=</span> <span class="n">sigma_x</span> <span class="o">+</span> <span class="n">sigma_y</span> <span class="o">+</span> <span class="n">C2</span>

    <span class="n">ssim_n</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">mu_x_mu_y</span> <span class="o">+</span> <span class="n">C1</span><span class="p">)</span> <span class="o">*</span> <span class="n">v1</span>
    <span class="n">ssim_d</span> <span class="o">=</span> <span class="p">(</span><span class="n">mu_x_sq</span> <span class="o">+</span> <span class="n">mu_y_sq</span> <span class="o">+</span> <span class="n">C1</span><span class="p">)</span> <span class="o">*</span> <span class="n">v2</span>
    <span class="n">ssim</span> <span class="o">=</span> <span class="n">ssim_n</span> <span class="o">/</span> <span class="n">ssim_d</span>

    <span class="k">return</span> <span class="n">ssim</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="MultiViewPhotometricLoss"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.MultiViewPhotometricLoss">[docs]</a><span class="k">class</span> <span class="nc">MultiViewPhotometricLoss</span><span class="p">(</span><span class="n">LossBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Self-Supervised multiview photometric loss.</span>
<span class="sd">    It takes two images, a depth map and a pose transformation to produce a</span>
<span class="sd">    reconstruction of one image from the perspective of the other, and calculates</span>
<span class="sd">    the difference between them</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    num_scales : int</span>
<span class="sd">        Number of inverse depth map scalesto consider</span>
<span class="sd">    ssim_loss_weight : float</span>
<span class="sd">        Weight for the SSIM loss</span>
<span class="sd">    occ_reg_weight : float</span>
<span class="sd">        Weight for the occlusion regularization loss</span>
<span class="sd">    smooth_loss_weight : float</span>
<span class="sd">        Weight for the smoothness loss</span>
<span class="sd">    C1,C2 : float</span>
<span class="sd">        SSIM parameters</span>
<span class="sd">    photometric_reduce_op : str</span>
<span class="sd">        Method to reduce the photometric loss</span>
<span class="sd">    disp_norm : bool</span>
<span class="sd">        True if inverse depth is normalized for</span>
<span class="sd">    clip_loss : float</span>
<span class="sd">        Threshold for photometric loss clipping</span>
<span class="sd">    progressive_scaling : float</span>
<span class="sd">        Training percentage for progressive scaling (0.0 to disable)</span>
<span class="sd">    padding_mode : str</span>
<span class="sd">        Padding mode for view synthesis</span>
<span class="sd">    automask_loss : bool</span>
<span class="sd">        True if automasking is enabled for the photometric loss</span>
<span class="sd">    kwargs : dict</span>
<span class="sd">        Extra parameters</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_scales</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">ssim_loss_weight</span><span class="o">=</span><span class="mf">0.85</span><span class="p">,</span> <span class="n">occ_reg_weight</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">smooth_loss_weight</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span>
                 <span class="n">C1</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span> <span class="n">C2</span><span class="o">=</span><span class="mf">9e-4</span><span class="p">,</span> <span class="n">photometric_reduce_op</span><span class="o">=</span><span class="s1">&#39;mean&#39;</span><span class="p">,</span> <span class="n">disp_norm</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">clip_loss</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span>
                 <span class="n">progressive_scaling</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">padding_mode</span><span class="o">=</span><span class="s1">&#39;zeros&#39;</span><span class="p">,</span>
                 <span class="n">automask_loss</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">=</span> <span class="n">num_scales</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">progressive_scaling</span> <span class="o">=</span> <span class="n">progressive_scaling</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ssim_loss_weight</span> <span class="o">=</span> <span class="n">ssim_loss_weight</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">occ_reg_weight</span> <span class="o">=</span> <span class="n">occ_reg_weight</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">smooth_loss_weight</span> <span class="o">=</span> <span class="n">smooth_loss_weight</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">C1</span> <span class="o">=</span> <span class="n">C1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">C2</span> <span class="o">=</span> <span class="n">C2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">photometric_reduce_op</span> <span class="o">=</span> <span class="n">photometric_reduce_op</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disp_norm</span> <span class="o">=</span> <span class="n">disp_norm</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clip_loss</span> <span class="o">=</span> <span class="n">clip_loss</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">padding_mode</span> <span class="o">=</span> <span class="n">padding_mode</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">automask_loss</span> <span class="o">=</span> <span class="n">automask_loss</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">progressive_scaling</span> <span class="o">=</span> <span class="n">ProgressiveScaling</span><span class="p">(</span>
            <span class="n">progressive_scaling</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)</span>

        <span class="c1"># Asserts</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">automask_loss</span><span class="p">:</span>
            <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">photometric_reduce_op</span> <span class="o">==</span> <span class="s1">&#39;min&#39;</span><span class="p">,</span> \
                <span class="s1">&#39;For automasking only the min photometric_reduce_op is supported.&#39;</span>

<span class="c1">########################################################################################################################</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">logs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns class logs.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;num_scales&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">,</span>
        <span class="p">}</span>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="MultiViewPhotometricLoss.warp_ref_image"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.MultiViewPhotometricLoss.warp_ref_image">[docs]</a>    <span class="k">def</span> <span class="nf">warp_ref_image</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inv_depths</span><span class="p">,</span> <span class="n">ref_image</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">ref_K</span><span class="p">,</span> <span class="n">pose</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Warps a reference image to produce a reconstruction of the original one.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        inv_depths : torch.Tensor [B,1,H,W]</span>
<span class="sd">            Inverse depth map of the original image</span>
<span class="sd">        ref_image : torch.Tensor [B,3,H,W]</span>
<span class="sd">            Reference RGB image</span>
<span class="sd">        K : torch.Tensor [B,3,3]</span>
<span class="sd">            Original camera intrinsics</span>
<span class="sd">        ref_K : torch.Tensor [B,3,3]</span>
<span class="sd">            Reference camera intrinsics</span>
<span class="sd">        pose : Pose</span>
<span class="sd">            Original -&gt; Reference camera transformation</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ref_warped : torch.Tensor [B,3,H,W]</span>
<span class="sd">            Warped reference image (reconstructing the original one)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">B</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">W</span> <span class="o">=</span> <span class="n">ref_image</span><span class="o">.</span><span class="n">shape</span>
        <span class="n">device</span> <span class="o">=</span> <span class="n">ref_image</span><span class="o">.</span><span class="n">get_device</span><span class="p">()</span>
        <span class="c1"># Generate cameras for all scales</span>
        <span class="n">cams</span><span class="p">,</span> <span class="n">ref_cams</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
            <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">DH</span><span class="p">,</span> <span class="n">DW</span> <span class="o">=</span> <span class="n">inv_depths</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span>
            <span class="n">scale_factor</span> <span class="o">=</span> <span class="n">DW</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">W</span><span class="p">)</span>
            <span class="n">cams</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Camera</span><span class="p">(</span><span class="n">K</span><span class="o">=</span><span class="n">K</span><span class="o">.</span><span class="n">float</span><span class="p">())</span><span class="o">.</span><span class="n">scaled</span><span class="p">(</span><span class="n">scale_factor</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">))</span>
            <span class="n">ref_cams</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Camera</span><span class="p">(</span><span class="n">K</span><span class="o">=</span><span class="n">ref_K</span><span class="o">.</span><span class="n">float</span><span class="p">(),</span> <span class="n">Tcw</span><span class="o">=</span><span class="n">pose</span><span class="p">)</span><span class="o">.</span><span class="n">scaled</span><span class="p">(</span><span class="n">scale_factor</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">))</span>
        <span class="c1"># View synthesis</span>
        <span class="n">depths</span> <span class="o">=</span> <span class="p">[</span><span class="n">inv2depth</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)]</span>
        <span class="n">ref_images</span> <span class="o">=</span> <span class="n">match_scales</span><span class="p">(</span><span class="n">ref_image</span><span class="p">,</span> <span class="n">inv_depths</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)</span>
        <span class="n">ref_warped</span> <span class="o">=</span> <span class="p">[</span><span class="n">view_synthesis</span><span class="p">(</span>
            <span class="n">ref_images</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">depths</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">ref_cams</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">cams</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
            <span class="n">padding_mode</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">padding_mode</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)]</span>
        <span class="c1"># Return warped reference image</span>
        <span class="k">return</span> <span class="n">ref_warped</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="MultiViewPhotometricLoss.SSIM"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.MultiViewPhotometricLoss.SSIM">[docs]</a>    <span class="k">def</span> <span class="nf">SSIM</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">kernel_size</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the SSIM (Structural SIMilarity) loss</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        x,y : torch.Tensor [B,3,H,W]</span>
<span class="sd">            Input images</span>
<span class="sd">        kernel_size : int</span>
<span class="sd">            Convolutional parameter</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ssim : torch.Tensor [1]</span>
<span class="sd">            SSIM loss</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ssim_value</span> <span class="o">=</span> <span class="n">SSIM</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">C1</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">C1</span><span class="p">,</span> <span class="n">C2</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">C2</span><span class="p">,</span> <span class="n">kernel_size</span><span class="o">=</span><span class="n">kernel_size</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">clamp</span><span class="p">((</span><span class="mf">1.</span> <span class="o">-</span> <span class="n">ssim_value</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">1.</span><span class="p">)</span></div>

<div class="viewcode-block" id="MultiViewPhotometricLoss.calc_photometric_loss"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.MultiViewPhotometricLoss.calc_photometric_loss">[docs]</a>    <span class="k">def</span> <span class="nf">calc_photometric_loss</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t_est</span><span class="p">,</span> <span class="n">images</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the photometric loss (L1 + SSIM)</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        t_est : list of torch.Tensor [B,3,H,W]</span>
<span class="sd">            List of warped reference images in multiple scales</span>
<span class="sd">        images : list of torch.Tensor [B,3,H,W]</span>
<span class="sd">            List of original images in multiple scales</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        photometric_loss : torch.Tensor [1]</span>
<span class="sd">            Photometric loss</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># L1 loss</span>
        <span class="n">l1_loss</span> <span class="o">=</span> <span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">t_est</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">images</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                   <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)]</span>
        <span class="c1"># SSIM loss</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ssim_loss_weight</span> <span class="o">&gt;</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="n">ssim_loss</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">SSIM</span><span class="p">(</span><span class="n">t_est</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">images</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">kernel_size</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
                         <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)]</span>
            <span class="c1"># Weighted Sum: alpha * ssim + (1 - alpha) * l1</span>
            <span class="n">photometric_loss</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">ssim_loss_weight</span> <span class="o">*</span> <span class="n">ssim_loss</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span> <span class="o">+</span>
                                <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">ssim_loss_weight</span><span class="p">)</span> <span class="o">*</span> <span class="n">l1_loss</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
                                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">photometric_loss</span> <span class="o">=</span> <span class="n">l1_loss</span>
        <span class="c1"># Clip loss</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_loss</span> <span class="o">&gt;</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">photometric_loss</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">(),</span> <span class="n">photometric_loss</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>
                <span class="n">photometric_loss</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span>
                    <span class="n">photometric_loss</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="nb">max</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">mean</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_loss</span> <span class="o">*</span> <span class="n">std</span><span class="p">))</span>
        <span class="c1"># Return total photometric loss</span>
        <span class="k">return</span> <span class="n">photometric_loss</span></div>

<div class="viewcode-block" id="MultiViewPhotometricLoss.reduce_photometric_loss"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.MultiViewPhotometricLoss.reduce_photometric_loss">[docs]</a>    <span class="k">def</span> <span class="nf">reduce_photometric_loss</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">photometric_losses</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Combine the photometric loss from all context images</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        photometric_losses : list of torch.Tensor [B,3,H,W]</span>
<span class="sd">            Pixel-wise photometric losses from the entire context</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        photometric_loss : torch.Tensor [1]</span>
<span class="sd">            Reduced photometric loss</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Reduce function</span>
        <span class="k">def</span> <span class="nf">reduce_function</span><span class="p">(</span><span class="n">losses</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">photometric_reduce_op</span> <span class="o">==</span> <span class="s1">&#39;mean&#39;</span><span class="p">:</span>
                <span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">l</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">losses</span><span class="p">])</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">losses</span><span class="p">)</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">photometric_reduce_op</span> <span class="o">==</span> <span class="s1">&#39;min&#39;</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">(</span><span class="n">losses</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="kc">True</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                    <span class="s1">&#39;Unknown photometric_reduce_op: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photometric_reduce_op</span><span class="p">))</span>
        <span class="c1"># Reduce photometric loss</span>
        <span class="n">photometric_loss</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">reduce_function</span><span class="p">(</span><span class="n">photometric_losses</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)])</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span>
        <span class="c1"># Store and return reduced photometric loss</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_metric</span><span class="p">(</span><span class="s1">&#39;photometric_loss&#39;</span><span class="p">,</span> <span class="n">photometric_loss</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">photometric_loss</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="MultiViewPhotometricLoss.calc_smoothness_loss"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.MultiViewPhotometricLoss.calc_smoothness_loss">[docs]</a>    <span class="k">def</span> <span class="nf">calc_smoothness_loss</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inv_depths</span><span class="p">,</span> <span class="n">images</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the smoothness loss for inverse depth maps.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        inv_depths : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">            Predicted inverse depth maps for all scales</span>
<span class="sd">        images : list of torch.Tensor [B,3,H,W]</span>
<span class="sd">            Original images for all scales</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        smoothness_loss : torch.Tensor [1]</span>
<span class="sd">            Smoothness loss</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Calculate smoothness gradients</span>
        <span class="n">smoothness_x</span><span class="p">,</span> <span class="n">smoothness_y</span> <span class="o">=</span> <span class="n">calc_smoothness</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">,</span> <span class="n">images</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)</span>
        <span class="c1"># Calculate smoothness loss</span>
        <span class="n">smoothness_loss</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([(</span><span class="n">smoothness_x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">abs</span><span class="p">()</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span> <span class="o">+</span>
                                <span class="n">smoothness_y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">abs</span><span class="p">()</span><span class="o">.</span><span class="n">mean</span><span class="p">())</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">**</span> <span class="n">i</span>
                               <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)])</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span>
        <span class="c1"># Apply smoothness loss weight</span>
        <span class="n">smoothness_loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">smooth_loss_weight</span> <span class="o">*</span> <span class="n">smoothness_loss</span>
        <span class="c1"># Store and return smoothness loss</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_metric</span><span class="p">(</span><span class="s1">&#39;smoothness_loss&#39;</span><span class="p">,</span> <span class="n">smoothness_loss</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">smoothness_loss</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="MultiViewPhotometricLoss.forward"><a class="viewcode-back" href="../../../losses/losses.multiview_photometric_loss.html#packnet_sfm.losses.multiview_photometric_loss.MultiViewPhotometricLoss.forward">[docs]</a>    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">image</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">inv_depths</span><span class="p">,</span>
                <span class="n">K</span><span class="p">,</span> <span class="n">ref_K</span><span class="p">,</span> <span class="n">poses</span><span class="p">,</span> <span class="n">return_logs</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">progress</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates training photometric loss.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        image : torch.Tensor [B,3,H,W]</span>
<span class="sd">            Original image</span>
<span class="sd">        context : list of torch.Tensor [B,3,H,W]</span>
<span class="sd">            Context containing a list of reference images</span>
<span class="sd">        inv_depths : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">            Predicted depth maps for the original image, in all scales</span>
<span class="sd">        K : torch.Tensor [B,3,3]</span>
<span class="sd">            Original camera intrinsics</span>
<span class="sd">        ref_K : torch.Tensor [B,3,3]</span>
<span class="sd">            Reference camera intrinsics</span>
<span class="sd">        poses : list of Pose</span>
<span class="sd">            Camera transformation between original and context</span>
<span class="sd">        return_logs : bool</span>
<span class="sd">            True if logs are saved for visualization</span>
<span class="sd">        progress : float</span>
<span class="sd">            Training percentage</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        losses_and_metrics : dict</span>
<span class="sd">            Output dictionary</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># If using progressive scaling</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">progressive_scaling</span><span class="p">(</span><span class="n">progress</span><span class="p">)</span>
        <span class="c1"># Loop over all reference images</span>
        <span class="n">photometric_losses</span> <span class="o">=</span> <span class="p">[[]</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)]</span>
        <span class="n">images</span> <span class="o">=</span> <span class="n">match_scales</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">inv_depths</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="p">(</span><span class="n">ref_image</span><span class="p">,</span> <span class="n">pose</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">poses</span><span class="p">)):</span>
            <span class="c1"># Calculate warped images</span>
            <span class="n">ref_warped</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">warp_ref_image</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">,</span> <span class="n">ref_image</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">ref_K</span><span class="p">,</span> <span class="n">pose</span><span class="p">)</span>
            <span class="c1"># Calculate and store image loss</span>
            <span class="n">photometric_loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_photometric_loss</span><span class="p">(</span><span class="n">ref_warped</span><span class="p">,</span> <span class="n">images</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="n">photometric_losses</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">photometric_loss</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
            <span class="c1"># If using automask</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">automask_loss</span><span class="p">:</span>
                <span class="c1"># Calculate and store unwarped image loss</span>
                <span class="n">ref_images</span> <span class="o">=</span> <span class="n">match_scales</span><span class="p">(</span><span class="n">ref_image</span><span class="p">,</span> <span class="n">inv_depths</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">)</span>
                <span class="n">unwarped_image_loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_photometric_loss</span><span class="p">(</span><span class="n">ref_images</span><span class="p">,</span> <span class="n">images</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                    <span class="n">photometric_losses</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">unwarped_image_loss</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="c1"># Calculate reduced photometric loss</span>
        <span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reduce_photometric_loss</span><span class="p">(</span><span class="n">photometric_losses</span><span class="p">)</span>
        <span class="c1"># Include smoothness loss if requested</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">smooth_loss_weight</span> <span class="o">&gt;</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="n">loss</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_smoothness_loss</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">,</span> <span class="n">images</span><span class="p">)</span>
        <span class="c1"># Return losses and metrics</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;loss&#39;</span><span class="p">:</span> <span class="n">loss</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span>
            <span class="s1">&#39;metrics&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">metrics</span><span class="p">,</span>
        <span class="p">}</span></div></div>

<span class="c1">########################################################################################################################</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Toyota Research Institute (TRI)

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>